package edu.neusoft.kongfanfei317.yuejian1.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

import edu.neusoft.kongfanfei317.yuejian1.R;
import edu.neusoft.kongfanfei317.yuejian1.adapter.MasterAdapter;
import edu.neusoft.kongfanfei317.yuejian1.bean.CollectBean;
import edu.neusoft.kongfanfei317.yuejian1.bean.MasterListBean;
import edu.neusoft.kongfanfei317.yuejian1.bean.RegisterBean;
import edu.neusoft.kongfanfei317.yuejian1.listener.ListListener;
import edu.neusoft.kongfanfei317.yuejian1.listener.TListener;
import edu.neusoft.kongfanfei317.yuejian1.model.CollectClubModel;
import edu.neusoft.kongfanfei317.yuejian1.model.MasterListModel;

import static edu.neusoft.kongfanfei317.yuejian1.R.id.shop_collect;

public class MasterListActivity extends BaseActivity {
    Button btn_back;
    String flag;
    int flag1;
    String user_id;
    TextView text;
    RecyclerView foodlist_recyclerView;
    private List<MasterListBean> items;
    String shop_id,shopname;
    ImageButton shop_collext;
    CollectClubModel collectShopModel;
    ListListener<MasterListBean> foodBeanListListener = new ListListener<MasterListBean>() {
        @Override
        public void onResponse(List<MasterListBean> list) {
            items=list;
            MasterAdapter adapter=new MasterAdapter(MasterListActivity.this,items,R.layout.card);
            foodlist_recyclerView.setAdapter(adapter);
            foodlist_recyclerView.setItemAnimator(new DefaultItemAnimator());
        }

        @Override
        public void onFail(String msg) {

        }
    };
    TListener <CollectBean> judgeListener=new TListener<CollectBean>() {
        @Override
        public void onResponse(CollectBean collectBean) {
            flag = collectBean.getCollected().toString();
            if (flag.equals("1")) {
                flag1 = 1;
                shop_collext.setImageResource(R.drawable.ic_collect_y);
            }
            else {
                flag1=0;
                shop_collext.setImageResource(R.drawable.ic_collect_n);
            }
        }

        @Override
        public void onFail(String msg) {
                showToast("error");
        }
    };

    TListener<RegisterBean> collcetListListener = new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess().equals("1")){
                if(flag1==1) {
                    shop_collext.setImageResource(R.drawable.ic_collect_n);
                    flag1 = 0;
                }
                else{
                    shop_collext.setImageResource(R.drawable.ic_collect_y);
                    flag1=1;
                }showToast("操作成功");

            }
            else{
                showToast("操作失败");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_master_list);
        btn_back = (Button) findViewById(R.id.btn_back);
        shop_collext = (ImageButton)findViewById(shop_collect);
        foodlist_recyclerView=(RecyclerView)findViewById(R.id.foodlist_recyclerView);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(MasterListActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        foodlist_recyclerView.setLayoutManager(linearLayoutManager);
        user_id = getUser_id();

    }

    @Override
    void initEvent() {

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                showToast("返回店铺列表");
            }
        });
        shop_collext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collectShopModel = new CollectClubModel();
            collectShopModel.collect(user_id,shop_id,collcetListListener);
            }
        });
    }

    @Override
    void initData() {
        Intent intent=getIntent();
        shop_id=intent.getStringExtra("id");
        //user_id = intent.getStringExtra("user_id");
        MasterListModel foodModel=new MasterListModel();
        foodModel.getFoodMessage(shop_id,foodBeanListListener);
        CollectClubModel collectShopModel=new CollectClubModel();
        collectShopModel.judge(user_id,shop_id,"0",judgeListener);

    }



  /*  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);
    }*/
}
